package com.yonyou.brigade.third_parties.yihao.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.third_parties.yihao.service.ICLYHApplyService;
import com.yonyou.brigade.util.resultutil.HttpSendUtils;
import com.yonyou.ybzboot.starter.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;

/**
 * Created by Administrator on 2018/10/20.
 */
@Service
public class CLYHApplyServiceImpl implements ICLYHApplyService {

    @Value("${yihao.url.addApprovalurl}")
    private  String addApproval_URL;

    private Logger logger= LoggerFactory.getLogger(CLYHApplyServiceImpl.class);

    @Override
    public void pushApplyVoucherToCLYH(String pushApplyStr) throws BusinessException {
        logger.info("推送差旅壹号申请单信息： " + pushApplyStr);
        String resultStr = HttpSendUtils.sendPost(addApproval_URL, pushApplyStr);
        logger.info("推送申请单差旅壹号返回信息： " + resultStr);
        if (!StringUtils.isEmpty(resultStr)){
            JSONObject resultJson = JSON.parseObject(resultStr);
            if (resultJson.getBoolean("successful")){
                logger.info("向差旅壹号推送数据:推送数据成功！");
            }else {
                logger.info("向差旅壹号推送数据:推送数据失败！");
                throw new BusinessException("差旅壹号推送数据:返回异常信息 " + resultJson.getString("errmsg"));
            }
        } else {
            throw new BusinessException("推送申请单差旅壹号无返回值");
        }
    }
}